#include "Thread.h"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <signal.h>
#include <assert.h>

  Thread::Thread():tid_(-1)
  {
    init();
  }

  Thread::~Thread()
  {
    fini();
  }

  void Thread::start(void)
  {
    int retVal=pthread_mutex_unlock(&lock_);
    assert(retVal==0);
  }

  void Thread::stop(void)
  {
    int retVal=pthread_cancel(tid_);
    assert(retVal==0);
  }

  void Thread::wait(void)
  {
    int retVal=pthread_join(tid_,NULL);
    assert(retVal==0);
  }

  void Thread::init()
  {
    int retVal=pthread_mutex_init(&lock_, NULL);
    assert(retVal==0);
    int lockRet=pthread_mutex_lock(&lock_);
    assert(lockRet==0);

    int err = pthread_create( &tid_, NULL, Thread::Run, this );
    assert(err==0);
  }

  void Thread::fini()
  {
    ///@todo complete the implementation
  }

  void* Thread::Run( void* obj )
  {
    Thread* This=(Thread*) obj;
    printf("(%s:%d) running\n", __FILE__,__LINE__);
    int lockRet=pthread_mutex_lock(&This->lock_);
    This->run();
  }


